package com.example.statistics.ui.sales

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.statistics.R
import com.example.statistics.bean.UserBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.constant.PreferenceConstants
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.preferenceDelete
import com.example.statistics.ex.preferenceFind
import com.example.statistics.ex.slimAdapter
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.LoginActivity
import com.example.statistics.utils.Constants
import com.jhj.slimadapter.SlimAdapter
import com.example.statistics.ex.contentText
import kotlinx.android.synthetic.main.activity_sales_product_search.*
import org.jetbrains.anko.dip
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class SalesCustomerSearchActivity : BaseActivity() {

    var adapter: SlimAdapter? = null
    val list = mutableListOf<String>()
    override val title: String
        get() = "豫德源"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sales_product_search)
        recycler_view.layoutManager = GridLayoutManager(this, 3)
        adapter = slimAdapter<String>(R.layout.list_item_text) {
            text(R.id.iv_text, it)
            clicked(R.id.iv_text) { _ ->
                startActivity<SalesCustomerListActivity>(Constants.TITLE to it)
            }
        }.attachTo(recycler_view).setDataList(list.toList())

        recycler_view.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                //不是第一个的格子都设一个左边和底部的间距
                outRect.left = dip(10);
                outRect.bottom = dip(10);
                //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
                if (parent.getChildLayoutPosition(view) % 3 == 0) {
                    outRect.left = 0;
                }
            }
        })
        val user = preferenceFind(UserBean::class.java, PreferenceConstants.USER)
        ic_search.onClick {
            val searchText = et_search.contentText
            if (searchText.isNullOrBlank()) {
                toast("请输入搜索的产品")
                return@onClick
            }
            if (list.indexOf(searchText) != -1) {
                list.remove(searchText)
            }
            startActivity<SalesCustomerListActivity>(Constants.TITLE to searchText)
            list.add(0, searchText)
            httpGet<List<String>>(UrlConfig.USER_SEARCH_MODIFY, config = {
                listParams = listOf("id" to user?.ID, "searchStr" to searchText)
            }) {}
        }

        tv_register.onClick {
            finish()
            startActivity<LoginActivity>()
            preferenceDelete(UserBean::class.java, PreferenceConstants.USER)
        }
    }

    override fun onResume() {
        super.onResume()
        adapter?.setDataList(list.toList())
    }
}